package com.zwj.service.impl;

import cn.hutool.core.util.StrUtil;
import com.zwj.entity.Classes;
import com.zwj.entity.Student;
import com.zwj.mapper.StudentMapper;
import com.zwj.service.IClassesService;
import com.zwj.service.IStudentService;
import com.zwj.util.PageResult;
import com.zwj.vo.StudentVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {
    @Autowired
    private  IClassesService classesService;

    @Override
    public PageResult<Student> findByPage(Integer page, Integer pageSize){
        Page<Student> page1 = this.page(new Page<>(page,pageSize));

        return new PageResult<>(page1.getRecords(), page1.getTotal());
    }

    @Override
    public PageResult<Student> search(Integer page, Integer pageSize, StudentVo studentVo) {
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(studentVo.getSname()),"sname", studentVo.getSname());
        wrapper.like(StrUtil.isNotBlank(studentVo.getAddr()),"addr", studentVo.getAddr());
        wrapper.eq(studentVo.getCid() != null && studentVo.getCid() != 0, "cid",studentVo.getCid());
        Page<Student> page1 = this.page(new Page<>(page,pageSize),wrapper);
        page1.getRecords().forEach(student -> {
            Classes cl = classesService.getById(student.getCid());
            student.setCname(cl.getCname());
        });
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }
}
